<html>
  <head>
<title>ROC Curve</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>ROC Curve</h3>
<%
        #set task id
   if(is.null(COOKIES$"task")){
   taskid <- format(Sys.time(), "%y%m%d%H%M%S")
   filepath <- file.path("/var/www/Rcloud/upload", taskid)
   dir.create(filepath)
   setwd(filepath)
   setCookie("task", taskid)
   #save(taskid, file=paste(filepath, "/", taskid, ".rda", sep=""))
   cat(paste("A new task created. (id:", taskid, ")"))
   }else{
   taskid <- unlist(strsplit(COOKIES$"task", "|", fixed=TRUE))[1]
   #setCookie("task", taskid)
     #taskid <- COOKIES$"task"
   filepath <- file.path("/var/www/Rcloud/upload", taskid)
   #dir.create(filepath)
   setwd(filepath)
   p<-paste("Task id: ", taskid, "<br>")
   cat(p)
   }
%>
<p>
<form name="data" action="#" method="GET">
  Select the prediction data:
  <select name="datap">
    <% #load listfile
       if(file.exists("listfile.rda")){
       load("listfile.rda")
       for(n in 1:length(filelist)){
       cat("<option value=", n, ">", filelist[[n]]$txt, "</option>")
       }
       }
    %>
  </select><br>
  Select the label data:
  <select name="datal">
    <% #load listfile
       if(file.exists("listfile.rda")){
       load("listfile.rda")
       for(n in 1:length(filelist)){
       cat("<option value=", n, ">", filelist[[n]]$txt, "</option>")
       }
       }
    %>
  </select><br>
  <input type="submit" value="Submit" />
</form>
<%
if(!is.null(GET$datap)){
   np <- GET$datap
   nl <- GET$datal
   task <- paste(taskid, np, nl, sep="|") #add data num to task
   setCookie("task", task)
   n1 <- as.numeric(np)
   n2 <- as.numeric(nl)
   cat("Prediction: ", filelist[[n1]]$txt, ". Label: ", filelist[[n2]]$txt, ".<br>", sep="")
   }
%>
<%
   if(grepl("|", COOKIES$"task", fixed=TRUE)){
   if(!is.na(unlist(strsplit(COOKIES$"task", "|", fixed=TRUE))[2])){
   np <- unlist(strsplit(COOKIES$"task", "|", fixed=TRUE))[2]
   nl <- unlist(strsplit(COOKIES$"task", "|", fixed=TRUE))[3]
   n1 <- as.numeric(np)
   n2 <- as.numeric(nl)
   cat("Prediction: ", filelist[[n1]]$txt, ". Label: ", filelist[[n2]]$txt, ".<br>", sep="")
   #cat("Data <I>", filelist[[N]]$txt, "</I> was selected.<br>")
   pp <- read.table(filelist[[n1]]$txt, header=as.logical(filelist[[n]]$head), sep=filelist[[n]]$sep)
   if(filelist[[n1]]$rownames=="first"){
   rownames(pp) <- as.character(pp[,1])
   pp <- as.data.frame(pp[, -1])
   }
   ll <- read.table(filelist[[n2]]$txt, header=as.logical(filelist[[n]]$head), sep=filelist[[n]]$sep)
   if(filelist[[n2]]$rownames=="first"){
   rownames(ll) <- as.character(ll[,1])
   ll <- as.data.frame(ll[, -1])
   }
}
}
%>
<p>
<form name="ROC" action="#" method="GET">
  ROC curve
  <select name="roc">
    <option value="tvf">true positive rate vs. false positive rate</option>
    <option value="svs">sensitivity vs. specificity</option>
    <option value="auc">area under curve</option>
    <option value="acc">accuracy vs. cutoff</option>
  </select>
  <input type="submit" value="Plot">
</form>
<%
   #library(ROCR)
   if(!is.null(GET$roc)){
   pp <- as.list(pp)
   ll <- as.list(ll)
   roc <- GET$roc
   col <- rainbow(length(pp))
   if(roc=="tvf"){
   pred<- prediction(pp, ll)
   perf <- performance(pred, "tpr", "fpr")
   rn <- as.numeric(Sys.time())
   png(paste("/var/www/images/R/", taskid, rn, "roc.png", sep=""))
   plot(perf, col=as.list(col), main="ROC curve")
   legend("bottomright", legend=names(pp), col=col, lty=1)
   dev.off()
   purl <- paste("/images/R/", taskid, rn, "roc.png", sep="")
   cat("<img src='", purl, "'/>", sep="")
   }else if(roc=="svs"){
   pred<- prediction(pp, ll)
   perf <- performance(pred, "sens", "spec")
   rn <- as.numeric(Sys.time())
   png(paste("/var/www/images/R/", taskid, rn, "roc.png", sep=""))
   plot(perf, col=as.list(col), main="ROC curve")
   legend("bottomleft", legend=names(pp), col=col, lty=1)
   dev.off()
   purl <- paste("/images/R/", taskid, rn, "roc.png", sep="")
   cat("<img src='", purl, "'/>", sep="")
   }else if(roc=="acc"){
   pred<- prediction(pp, ll)
   perf <- performance(pred, "acc")
   rn <- as.numeric(Sys.time())
   png(paste("/var/www/images/R/", taskid, rn, "roc.png", sep=""))
   plot(perf, col=as.list(col), main="Accuracy over cutoffs")
   legend("bottomright", legend=names(pp), col=col, lty=1)
   dev.off()   
   purl <- paste("/images/R/", taskid, rn, "roc.png", sep="")
   cat("<img src='", purl, "'/>", sep="")
   }else if(roc=="auc"){
   pred<- prediction(pp, ll)
   perf <- performance(pred, "auc")
   area <- unlist(perf@"y.values")
   names(area) <- names(pp)
   rn <- as.numeric(Sys.time())
   png(paste("/var/www/images/R/", taskid, rn, "roc.png", sep=""))
   ap <- barplot(area, col=col, main="AUCs")
   text(ap, area+0.01, format(area), xpd=TRUE)
   dev.off()
   purl <- paste("/images/R/", taskid, rn, "roc.png", sep="")
   cat("<img src='", purl, "'/>", sep="")
   }
   #purl <- paste("/images/R/", taskid, "roc.png", sep="")
   #cat("<img src='", purl, "'/>", sep="")
}
%>


<address>
<a href="mailto:root@bme.localdomain">HQ</a>
</address>
</body>
</html>
